#!/bin/sh -e

# dir of distubtion system (we assume $PWD is distrubtion root
DIST_SYSTEM=dist_system
#### Config ###
while true ; do 
  if [ -e $DIST_SYSTEM ] ;then 
    break
  else
    cd ..
  fi
done


. $DIST_SYSTEM/etc/PROJECT_INFO


sh -c "cd $DIST_DIR;. \$PWD/dist_clean"
 

BUILD_FILE=$PROJECT_NAME-$PROJECT_VER${ARCHIVE_ENDING} # use projekt $VER when build src archive 
######################
### excute part ###

mkdir $BUILD_FOLDER
if ls $DIST_SYSTEM/etc/source_clean.d/*.sh > /dev/null 2>&1 ; then
        for script in $DIST_SYSTEM/etc/source_clean.d/*.sh ; do
                sh $script
        done
fi

# only list  directorys and files that are in root to copy them in to $BUILD_TMP and exclude backups (files with ~ at the end) #  -e 's/^.//' -e  's/^\///'
for DIST_FILE in $DIST_FILES ; do  
  if [ -d $DIST_FILE ] ; then
    for file in $( find $DIST_FILE ! -iname '*~' ! -name  '*.tar.*'  | sed -e '1 d' -e 's/^.\///') ; do 
      mkdir -p $BUILD_FOLDER/$( dirname $file ); 
      if [ ! -d $file ] || [ -L $file ] ; then
	cp -P $file $BUILD_FOLDER/$file ;
      fi 
    done
  else
    cp -P $DIST_FILE $BUILD_FOLDER/.
  fi
done
if ls $DIST_SYSTEM/etc/build_src.d/*.sh > /dev/null 2>&1 ; then
        for script in $DIST_SYSTEM/etc/build_src.d/*.sh ; do
                sh $script
        done
fi
# cd $BUILD_TMP
# clean uneeeded archives in our source dist
# rm -rf dist/pacman/*.tar.xz
# rm -rf dist/pacman/{pkg,src}
# rm -rf dist/source/*$ARCHIVE_ENDING
# rm -rf dist/source/${PROJECT_NAME}_latest # remove link to latest archive too
# rm -rf ${PROJECT_NAME}_latest$ARCHIVE_ENDING
# rm -rf build_src.sh
tar -caf $SOURCE_DIST/$BUILD_FILE $BUILD_FOLDER
#mv $BUILD_FILE ../$SOURCE_DIST/$BUILD_FILE
#cd .. # go back to source dir
rm -rf $BUILD_FOLDER

rm -rf $SOURCE_DIST/${PROJECT_NAME}_latest
rm $SOURCE_DIST/${PROJECT_NAME}_latest$ARCHIVE_ENDING
ln -s $BUILD_FILE $SOURCE_DIST/${PROJECT_NAME}_latest$ARCHIVE_ENDING # link to latest source
ln -s $PWD/$SOURCE_DIST/${PROJECT_NAME}_latest$ARCHIVE_ENDING ${PROJECT_NAME}_latest$ARCHIVE_ENDING # same but now in $PROJECT_NAME root
